<template>
  <div class="searchbox">
    <div class="header">
      <div class="history flexBet">
        <span>历史搜索</span>
        <van-icon name="delete-o" size="22" color="#000" @click="clearHistory"></van-icon>
      </div>
      <div class="headertag">
        <van-tag
        class="margin10"
          color="#FFE0B2"
          round
          plain
          v-for="(item, index) in historyarr"
          :key="index"
          >{{ item }}</van-tag
        >
      </div>
    </div>
    <div class="box"></div>
    <div class="content">
      <div class="searchContainer">
        <van-search
          v-model="key"
          placeholder="看看哪里不舒服"
          shape="round"
          background="#f4f4f4"
          @search="search"
          @cancel="onCancel"
        />
      </div>
      <div class="recommend">
        <span>试试下面</span>
        <div class="ic">
          <van-tag
          class="margin10"
            v-for="(item, index) in commandTags"
            :key="index"
            @click="recommendSearch(item)"
            color="#FF9800"
            >{{ item }}</van-tag
          >
        </div>
      </div>
    </div>
    <van-popup
      v-model="show"
      closeable
      round
      position="bottom"
      :style="{ height: '90%' }"
    >
      <van-tabs
        class="tabs"
        sticky
        animated
        v-model="Tabactive"
        background="#FDFFF7"
      >
        <!-- 新闻 -->
        <van-tab title="新闻">
          <van-list
            v-model="article.loading"
            :finished="article.finished"
            finished-text="没有更多的搜索结果"
            error-text="error"
            :offset="10"
            :immediate-check="false"
          >
            <art v-for="item in articleList" :key="item.id" :post="item"></art>
          </van-list>
        </van-tab>

        <!-- 阅读推荐 -->
        <van-tab title="书籍">
          <van-list
            v-model="book.loading"
            :finished="book.finished"
            finished-text="没有更多的搜索结果"
            error-text="error"
            :offset="10"
            :immediate-check="false"
          >
            <bookcard
              class="bookcard"
              v-for="value in bookList"
              :key="value.id"
              :book="value"
              style="margin-bottom: 10px"
            ></bookcard>
          </van-list>
        </van-tab>
      </van-tabs>
    </van-popup>
  </div>
</template>

<script>
import bookcard from "@/components/bookcard";
import lock from "@/components/lock";
import art from "@/components/articlecard";
export default {
  data() {
    return {
      commandTags: ["中暑", "头疼", "人参", "天池穴"],
      show: false,
      Tabactive: 0,
      key: "",
      bookList: [],
      book: {
        loading: false,
        finished: true,
      },
      article: {
        loading: false,
        finished: true,
      },
      articleList: [],
      historyarr: ["人参", "头疼", "上火"],
    };
  },
  components: {
    art,
    lock,
    bookcard,
  },
  methods: {
    clearHistory() {
      this.historyarr = [];
    },
    recommendSearch(item) {
      (this.key = item), this.search();
    },
    tosearch(key) {
      this.$http.get(`/search/${key}`).then((res) => {
        let data = res.data.result;
        console.log(data);
        this.bookList = data.bookList;
        this.articleList = data.articleList;
        this.articleList.forEach((element) => {
          element.cover = [element.photoa, element.photob, element.photoc];
        });
        this.show = true;
      });
    },
    //页面跳转
    jump(id) {
      this.$router.push({ path: "/articaldetail/" + id });
    },
    // 历史中搜索
    clicksearch(v) {
      this.key = v;
      this.search();
    },
    search() {
      if (this.key.trim().length == 0) {
        this.$toast.fail("请输入搜索关键字");
      } else {
        this.tosearch(this.key);
        // 判断historyarr数组中是否已经存在该关键字
        let has = this.historyarr.indexOf(this.key);
        if (has != -1) {
          //有便删除
          this.historyarr.splice(has, 1);
        }
        // 将新的搜索关键字添加到历史数组中并存储到本地储备
        this.historyarr.unshift(this.key);
        localStorage.setItem("keyword", JSON.stringify(this.historyarr));
      }
    },
    onCancel() {},
  },
};
</script>

<style lang="less" scoped>
.flexBet {
  display: flex;
  justify-content: space-between;
}
.margin10{
  margin-right: 10px;
}
.tabs {
  padding-top: 40px;
}
.header {
  padding: 40px 30px 0;
  font-size: 18px;
  .van-tag {
    margin-top: 20px;
    font-size: 20px;
    line-height: 20px;
  }
}
.recommend {
  padding: 20px 30px 20px;
  font-size: 18px;
  margin-top: 110px;
  .ic {
    margin-top: 20px;
    .van-tag {
      font-size: 20px;
      line-height: 20px;
    }
  }
}

.searchContainer {
  //  padding: 0 12px;
  .van-cell {
    line-height: 54px;
    font-size: 16px;
    .van-field__left-icon .van-icon,
    .van-field__right-icon .van-icon {
      display: block;
      font-size: 24px;
      line-height: 54px;
    }
  }
  .van-search__content--round {
    border-radius: 20px;
  }
}

.toback {
  position: relative;
}

.box {
  height: 85px;
}
.content {
  // background: #000;
  height: 500px;
  .history {
    padding: 20px;
    // height: 100px;
    border-bottom: 1px solid #ccc;
    div {
      font-size: 18px;
      display: flex;
      justify-content: space-between;
      .del {
        display: inline-block;
        margin-top: 10px;
        padding: 0 10px;
        background: #eee;
        height: 20px;
        line-height: 20px;
        border-radius: 10px;
        font-size: 14px;
      }
    }
    > span {
      display: inline-block;
      margin: 10px;
      padding: 0 10px;
      background: #eee;
      height: 20px;
      line-height: 20px;
      border-radius: 10px;
      color: #888;
      font-size: 14px;
    }
  }
  .result {
    padding: 20px;
    // height: 100px;
    div {
      font-size: 18px;
    }
    .list {
      margin-top: 10px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      padding: 0 10px;
      background: #eee;
      height: 25px;
      line-height: 25px;
      border-radius: 10px;
      font-size: 18px;
      text-decoration: underline; //下划线
    }
  }
}
</style>